package com.qiyi.player.core.ad
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.core.IDestroy;
   import com.qiyi.player.core.video.decoder.AdDecoder;
   import com.qiyi.player.core.ad.render.AdRender;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.core.video.file.actors.datatile.MediaData;
   import flash.geom.Rectangle;
   import com.qiyi.player.core.video.def.DecoderStatusEnum;
   import flash.media.SoundTransform;
   import com.qiyi.player.core.Config;
   import flash.events.NetStatusEvent;
   import com.qiyi.player.core.video.events.DecoderEvent;
   import com.qiyi.player.core.player.def.StatusEnum;
   import flash.display.Sprite;
   import com.qiyi.player.base.logging.Log;
   
   public class AdPlayerProxy extends EventDispatcher implements IDestroy
   {
      
      public function AdPlayerProxy(param1:Sprite, param2:ICorePlayer) {
         this._log = Log.getLogger("com.qiyi.player.core.ad.AdPlayerProxy");
         super();
         this._holder = param2;
         this._render = new AdRender(param1);
      }
      
      private var _decoder:AdDecoder;
      
      private var _render:AdRender;
      
      private var _fillEnd:Boolean;
      
      private var _volume:int = 100;
      
      private var _smoothVolume:int;
      
      private var _adIndex:int = -1;
      
      private var _adId:String;
      
      private var _isPlaying:Boolean;
      
      private var _isReading:Boolean;
      
      private var _holder:ICorePlayer;
      
      private var _isEndSequence:Boolean;
      
      protected var _log:ILogger;
      
      public function get fillEnd() : Boolean {
         return this._fillEnd;
      }
      
      public function set fillEnd(param1:Boolean) : void {
         if(this._fillEnd != param1)
         {
            this._fillEnd = param1;
         }
      }
      
      public function setEndSequence() : void {
         if((this._fillEnd) && !this._isEndSequence)
         {
            if(this._decoder)
            {
               this._log.debug("[AdPlayerProxy.setEndSequence] endsquence id = " + this._adId);
               this._decoder.endSequence();
               this._isEndSequence = true;
            }
         }
      }
      
      public function get adIndex() : int {
         return this._adIndex;
      }
      
      public function set adIndex(param1:int) : void {
         this._adIndex = param1;
      }
      
      public function get adId() : String {
         return this._adId;
      }
      
      public function set adId(param1:String) : void {
         if(this._adId != param1)
         {
            this._adId = param1;
         }
      }
      
      public function get isPlaying() : Boolean {
         return this._isPlaying;
      }
      
      public function get isReading() : Boolean {
         return this._isReading;
      }
      
      public function set isReading(param1:Boolean) : void {
         this._isReading = param1;
      }
      
      public function get time() : int {
         if(this._decoder)
         {
            return this._decoder.time;
         }
         return 0;
      }
      
      public function get volume() : int {
         return this._volume;
      }
      
      public function reset() : void {
         this.closeDecoderListener();
         if(this._decoder)
         {
            this._decoder.pause();
         }
         this._render.show(false);
         this._render.clear();
         this._fillEnd = false;
         this._adIndex = -1;
         this._adId = "";
         this._isPlaying = false;
         this._isReading = false;
         this._isEndSequence = false;
      }
      
      public function updateDecoder() : void {
         this.destroyDecoder();
         this.createDecoder();
      }
      
      public function appendData(param1:MediaData) : void {
         if(this._decoder)
         {
            this._isEndSequence = false;
            this._decoder.appendData(param1);
         }
      }
      
      public function getADArea() : Rectangle {
         return this._render.getADArea();
      }
      
      public function setADRect(param1:int, param2:int, param3:int, param4:int) : void {
         this._render.setADRect(param1,param2,param3,param4);
      }
      
      public function setADVolume(param1:int) : void {
         this._volume = param1;
         this.updateVolume(this._volume);
      }
      
      public function smoothVolume(param1:int) : void {
         if(this._smoothVolume != param1)
         {
            this._smoothVolume = param1;
            this.updateVolume(this._smoothVolume);
         }
      }
      
      public function playAd() : void {
         this.restoreVolume();
         this.openDecoderListener();
         if(this._decoder.status == DecoderStatusEnum.STOPPED)
         {
            this._decoder.play(null);
         }
         else
         {
            this._decoder.resume();
         }
         this._render.show(true);
         this._isPlaying = true;
      }
      
      public function pauseAd() : void {
         if(this._decoder)
         {
            this._decoder.pause();
         }
      }
      
      public function stopAd() : void {
         if(this._isPlaying)
         {
            this.closeDecoderListener();
            if(this._decoder)
            {
               this._decoder.pause();
            }
            this._render.show(false);
            this._render.clear();
            this._fillEnd = false;
            this._adId = "";
            this._isPlaying = false;
            this._isReading = false;
         }
      }
      
      public function seekAd() : void {
      }
      
      public function resumeAd() : void {
         if(this._decoder)
         {
            this._decoder.resume();
         }
      }
      
      public function destroy() : void {
         this.closeDecoderListener();
         if(this._decoder)
         {
            this._decoder.destroy();
         }
         this._render.destroy();
         this._decoder = null;
         this._render = null;
         this._holder = null;
      }
      
      private function updateVolume(param1:Number) : void {
         var _loc2_:SoundTransform = null;
         if(this._decoder)
         {
            _loc2_ = new SoundTransform();
            _loc2_.volume = param1 / 100;
            this._decoder.soundTransform = _loc2_;
         }
      }
      
      private function createDecoder() : void {
         this._decoder = new AdDecoder(this._holder);
         this._decoder.bufferTime = Config.STREAM_SHORT_BUFFER_TIME / 1000;
         this.updateVolume(this._volume);
         this._render.bind(this._decoder);
         this._decoder.play(null);
         this._decoder.pause();
      }
      
      private function destroyDecoder() : void {
         if(this._decoder)
         {
            this.closeDecoderListener();
            try
            {
               this._render.bind(null);
               this._decoder.destroy();
            }
            catch(error:Error)
            {
               _log.error("AdPlayerProxy destroyDecoder error!");
            }
            this._decoder = null;
         }
      }
      
      private function openDecoderListener() : void {
         if(this._decoder)
         {
            this._decoder.addEventListener(NetStatusEvent.NET_STATUS,this.onDecoderNetStatus);
            this._decoder.addEventListener(DecoderEvent.Evt_StatusChanged,this.onDecoderStatusChanged);
            this.onDecoderStatusChanged(null);
         }
      }
      
      private function closeDecoderListener() : void {
         if(this._decoder)
         {
            this._decoder.removeEventListener(NetStatusEvent.NET_STATUS,this.onDecoderNetStatus);
            this._decoder.removeEventListener(DecoderEvent.Evt_StatusChanged,this.onDecoderStatusChanged);
         }
      }
      
      private function onDecoderNetStatus(param1:NetStatusEvent) : void {
         switch(param1.info.code)
         {
            case "NetStream.Buffer.Flush":
               break;
            case "NetStream.Play.Stop":
               trace("===========================> NetStream.Play.Stop");
               break;
            case "NetStream.Buffer.Empty":
               if(this._fillEnd)
               {
                  dispatchEvent(new AdEvent(AdEvent.Evt_BufferEmpty));
               }
               break;
         }
      }
      
      private function onDecoderStatusChanged(param1:DecoderEvent) : void {
         var _loc2_:* = 0;
         if(this._decoder)
         {
            _loc2_ = -1;
            switch(this._decoder.status)
            {
               case DecoderStatusEnum.PLAYING:
                  _loc2_ = StatusEnum.AD_PLAYING;
                  break;
               case DecoderStatusEnum.PAUSED:
                  _loc2_ = StatusEnum.AD_PAUSED;
                  break;
               case DecoderStatusEnum.SEEKING:
                  _loc2_ = StatusEnum.AD_SEEKING;
                  break;
               case DecoderStatusEnum.WAITING:
                  _loc2_ = StatusEnum.AD_WAITING;
                  break;
            }
            if(_loc2_ > 0)
            {
               dispatchEvent(new AdEvent(AdEvent.Evt_ADStatusChanged,_loc2_));
            }
         }
      }
      
      private function restoreVolume() : void {
         if((this._volume) && (this._smoothVolume) && !(this._volume == this._smoothVolume))
         {
            this.setADVolume(this._volume);
         }
      }
   }
}
